package com.mkx.core.factory;

import com.mkx.core.wd.ThreadPoolWatchDog;
import com.mkx.core.wd.TimeWheelWatchDog;
import com.mkx.core.WatchDog;
import com.mkx.properties.RedisLockProperties;
import org.springframework.data.redis.core.RedisTemplate;

public class WatchDogFactory {
    
    public static WatchDog createWatchDog(RedisLockProperties properties,
                                          RedisTemplate<String, Object> redisTemplate,
                                          String lockKey,
                                          String lockValue) {
        if (!properties.isWatchDogEnabled()) {
            return new NoOpWatchDog();
        }
        
        return switch (properties.getWatchDogStrategy()) {
            case THREAD_POOL -> new ThreadPoolWatchDog(
                redisTemplate, lockKey, lockValue, properties.getWatchDogInterval());
                
            case TIME_WHEEL -> new TimeWheelWatchDog(
                redisTemplate, lockKey, lockValue, properties.getWatchDogInterval(),
                properties.getTimeWheel().getTickDuration());
        };
    }
    
    // 空操作的 WatchDog，当看门狗被禁用时使用
    private static class NoOpWatchDog implements WatchDog {
        @Override
        public void start() {}
        @Override
        public void stop() {}
        @Override
        public boolean isRunning() { return false; }
    }
}